package com.example.musicplayer;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.musicplayer.model.Music;

import java.util.ArrayList;


public class MusiclistFragment extends Fragment {
    ListView listView;
    MusiclistAdapter musiclistAdapter;
    public static ArrayList<Music> dataList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Init();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_musiclist, container, false);

        listView = view.findViewById(R.id.music_list);
        //初始化音乐播放列表
        musiclistAdapter = new MusiclistAdapter(requireContext(), R.layout.list_item, dataList);
        listView.setAdapter(musiclistAdapter);
        // list的item触发click
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//                Bundle bundle=new Bundle();
                Music music = dataList.get(position);
                Intent intent = new Intent(MusiclistFragment.this.getContext(), MainActivity.class);
                intent.putExtra("pos", position);
                intent.putExtra("singerName", music.getSingerName());
                intent.putExtra("musicName", music.getMusicName());

                startActivity(intent);
                System.out.println("获取上一个活动" + getActivity());
//                getActivity().finish();
            }
        });
        return view;
    }

    void Init() {
        // 初始化一下歌曲列表
        dataList = new ArrayList<>();
        Music item1 = new Music("我是歌曲1", "张三", "music_01");
        Music item2 = new Music("我是歌曲2", "张三", "music_02");
        Music item3 = new Music("我是歌曲3", "张三", "music_03");

        dataList.add(item1);
        dataList.add(item2);
        dataList.add(item3);
    }
}